package com.becca.cake.controller.category;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.becca.cake.core.common.BaseAction;
import com.becca.cake.core.common.PagingBean;
import com.becca.cake.core.exception.CommonExceptionHandle;
import com.becca.cake.model.category.GoodCategoryModel;
import com.becca.cake.service.category.IGoodCategoryService;
import com.becca.cake.util.JsonUtil;


/**
 * 抵用券Controller
 * @copyright  尊星(上海)信息科技有限公司
 * @author mouwanming
 * @date 2016年9月22日 下午10:12:37
 */
@Controller
@RequestMapping("/goodCategoryManager")
public class GoodCategoryAction extends BaseAction
{
	@Autowired
	IGoodCategoryService iGoodCategoryService;
	
	/**
	 * 查询分类名称
	 * @author mouwanming
	 * @return
	 */
	@RequestMapping(value="/selectGoodCategoryName",method = RequestMethod.POST)
	@ResponseBody
	public String selectGoodCategoryName(GoodCategoryModel goodCategoryModel,
			@RequestParam(value = "existall", required = false) String existall){
		try
		{
			List<GoodCategoryModel> list = this.iGoodCategoryService.selectGoodCategoryName();
			if (existall.equals("true")) {
				GoodCategoryModel g = new GoodCategoryModel();
                g.setCategoryName("全部");
                list.add(g);
            }
			return JsonUtil.writeObjectNoPageJSON(list);
		} catch (Exception e)
		{
			return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
		}
	}
	
	/**
	 * 查询所有分类
	 * @param goodCategoryModel
	 * @return
	 */
	@RequestMapping(value="/selectGoodCategory",method = RequestMethod.POST)
	@ResponseBody
	public String selectGoodCategory(GoodCategoryModel goodCategoryModel){
		PagingBean page = getInitPagingBean();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("goodCategoryModel", goodCategoryModel);
		map.put("pagingBean", page);
		try
		{
			List<GoodCategoryModel> list = iGoodCategoryService.selectGoodCategory(map);
			Long count = iGoodCategoryService.selectGoodCategoryCount(map);
			return JsonUtil.writeObjectPageJSON(list, count);
		} catch (Exception e)
		{
			return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
		}
	}
	
	/**
	 * 添加或修改分类信息
	 * @param goodCategoryModel
	 * @return
	 */
	@RequestMapping(value = "/insertOrUpdateGoodCategory", method = RequestMethod.POST )
	@ResponseBody
	public String insertOrUpdateGoodCategory(GoodCategoryModel goodCategoryModel){
		
		try
		{
			this.iGoodCategoryService.insertOrUpdateGoodCategory(goodCategoryModel, this.getUserSeesion());
			return JsonUtil.returnJsonSuccess("操作分类信息成功!");
		} catch (Exception e)
		{
			return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
		}
		
	}
	
	
	/**
     * 根据id查询分类对象
     * @param id
     * @return
     */
    @RequestMapping(value = "/selectGoodCategoryById", method = RequestMethod.POST)
    @ResponseBody
    public String selectGoodCategoryById(@RequestParam("id") Integer id) {
        try {
            //step1:请求参数
        	GoodCategoryModel goodCategoryModel = this.iGoodCategoryService.selectGoodCategoryById(id);
            return JsonUtil.writeObjectFormJSON(goodCategoryModel);
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }
    
    /**
     * 根据id启用或禁用分类对象
     * @param id
     * @return
     */
    @RequestMapping(value = "/updateGoodCategoryById", method = RequestMethod.POST)
    @ResponseBody
    public String updateGoodCategoryById(@RequestParam(value = "id", required = false) Integer id,
                                      @RequestParam(value = "categoryStatus", required = false) String categoryStatus) {
        try {
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("id", id);
            paramMap.put("categoryStatus", categoryStatus);
            paramMap.put("userName", this.getUserSeesion().getTrueName());
            this.iGoodCategoryService.updateGoodCategoryById(paramMap);
            return JsonUtil.returnJsonSuccess("操作分类信息成功!");
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
